<template>
	<view class="content">
		<button class="size" @tap="startBeaconDiscovery">开始搜索</button>
		<button class="size" @tap="stopBeaconDiscovery">停止搜索附近的iBeacon设备</button>
		<button class="size" @tap="getBeacons">获取已搜索到的iBeacon设备</button>
		<button class="size" @tap="onBeaconUpdate">监听iBeacon设备更新</button>
		<button class="size" @tap="onBeaconServiceChange">监听iBeacon服务状态变化</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: "",
				channels: []
			}
		},
		onLoad() {

		},
		methods: {
			startBeaconDiscovery() {
				plus.ibeacon.startBeaconDiscovery({
					success: (e) => {
						plus.nativeUI.alert('start success' + JSON.stringify(e));
					},
					fail: (e) => {
						plus.nativeUI.alert('start error: ' + JSON.stringify(e));
					}
				})

			},
			stopBeaconDiscovery() {
				plus.ibeacon.stopBeaconDiscovery({
					success: function() {
						plus.nativeUI.alert('stop success');
					},
					failed: function(e) {
						plus.nativeUI.alert('stop error: ' + JSON.stringify(e));
					}
				});
			},
			getBeacons() {
				plus.ibeacon.startBeaconDiscovery({
					success: function() {
						plus.ibeacon.getBeacons({
							success: function(e) {
								var beacons = e.beacons;
								var uuids = null;
								for (var i in beacons) {
									if (uuids) {
										uuids += ', ' + beacons[i].uuid;
									} else {
										uuids = beacons[i].uuid;
									}
								}
								plus.nativeUI.alert('Beacons: ' + uuids);
							},
							failed: function(e) {
								plus.nativeUI.alert('getBeacons error: ' + JSON.stringify(e));
							}
						});
					},
					failed: function(e) {
						plus.nativeUI.alert('start error: ' + JSON.stringify(e));
					}
				});
			},
			onBeaconUpdate() {
				plus.ibeacon.startBeaconDiscovery({
					success: function() {
						plus.ibeacon.onBeaconUpdate(function(e) {
							var beacons = e.beacons;
							var uuids = null;
							for (var i in beacons) {
								if (uuids) {
									uuids += ', ' + beacons[i].uuid;
								} else {
									uuids = beacons[i].uuid;
								}
							}
							plus.nativeUI.alert('Beacons updated: ' + uuids);
						});
					},
					failed: function(e) {
						plus.nativeUI.alert('start error: ' + JSON.stringify(e));
					}
				});

			},
			onBeaconServiceChange() {
				plus.ibeacon.onBeaconServiceChange(function(e) {
					plus.nativeUI.alert('BeaconServiceChanged: available=' + e.available + ', discovering=' + e
						.discovering);
				});
			}

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.size {
		margin: 30rpx 0;
	}
</style>
